﻿namespace Arms.Presentation.Web.Models.Shared
{
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.Web.Mvc;

    public class ProductPickerBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var products = JsonConvert.DeserializeObject<List<ProductPickerProductViewModel>>(bindingContext.ValueProvider.GetValue("ProductPicker.Products").AttemptedValue);
            var baseModel = base.BindModel(controllerContext, bindingContext);
            var model = baseModel as ProductPickerModel;
            model.Products = new List<ProductPickerProductViewModel>(products);
            return model;
        }
    }
}